/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author shinji
 */
@Named(value = "a_rightsManagedBean")
@SessionScoped
public class RightsManagedBean implements Serializable {

    @EJB
    private RoleRightsFacade roleRightsFacade;
    @EJB
    private RightsFacade rightsFacade;
    private List<Rights> list = new ArrayList<>();
    private List<String> selectListRights = new ArrayList<>();
    private Map<String, String> mapRights = new HashMap<>();
    private Rights obj = new Rights();
    private Role objR = new Role();
    private String Name;

    public RightsFacade getRightsFacade() {
        return rightsFacade;
    }

    public void setRightsFacade(RightsFacade rightsFacade) {
        this.rightsFacade = rightsFacade;
    }

    public List<String> getSelectListRights() {
        return selectListRights;
    }

    public void setSelectListRights(List<String> selectListRights) {
        this.selectListRights = selectListRights;
    }

    public Map<String, String> getMapRights() {
        return mapRights;
    }

    public void setMapRights(Map<String, String> mapRights) {
        this.mapRights = mapRights;
    }

    public List<Rights> getList() {
        return list;
    }

    public void setList(List<Rights> list) {
        this.list = list;
    }

    public Rights getObj() {
        return obj;
    }

    public void setObj(Rights obj) {
        this.obj = obj;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * Creates a new instance of RightsManagedBean
     */
    public RightsManagedBean() {
    }

    /**
     * ********************************************
     */
    public void create() {
        try {
            obj = new Rights();
            obj.setName(Name);

            this.rightsFacade.create(obj);
            set_list();
            Name = "";

            set_message(FacesMessage.SEVERITY_INFO, "Create Rights", "Create " + obj.getName() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Rights", "Create " + obj.getName() + " error !!!");
        }
    }

    public void edit() {
        try {
            this.rightsFacade.edit(obj);
            
            
            set_message(FacesMessage.SEVERITY_INFO, "Edit Religion", "Edit " + obj.getName() + " successfull.");         
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Religion", "Edit " + obj.getName() + " error !!!");
        }
    }

    public void remove(Rights r) {
        try {
            this.rightsFacade.remove(r);
            set_list();

            set_message(FacesMessage.SEVERITY_INFO, "Remove Rights", "Remove " + obj.getName() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Rights", "Remove " + obj.getName() + " error !!!");
        }
    }

    /**
     * ********************************************
     */
    public Map<String, String> set_selectCheckboxMenu() {
        try {
            for (Rights r : list) {
                mapRights.put(r.getName(), r.getId().toString());
            }

            return mapRights;
        } catch (Exception e) {
            return null;
        }
    }

    public void set_selectListRights(Role r) {
        selectListRights.clear();
        List<String> temp = new ArrayList<String>();

        for (RoleRights rr : roleRightsFacade.findAll()) {
            
            if (rr.getAgree() && rr.getRoleRightsPK().getIDRole() == r.getId()) {
                temp.add(String.valueOf(rr.getRoleRightsPK().getIDRights()));
            }
        }

        selectListRights = temp;
    }

    public void set_list() {
        try {
            list = this.rightsFacade.findAll();
        } catch (Exception e) {
            list = null;
        }
    }

    private void set_message(FacesMessage.Severity severity, String title, String message) {
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
}
